import { Component, OnInit } from '@angular/core';
import { myValidNull } from '@core/util/string-utils';
import { MyModalHelper } from '@core/my-modal.helper';
import { NzMessageService } from 'ng-zorro-antd';
import { ActivatedRoute } from '@angular/router';
import { CompanyTypeService } from '@core/routes-services/company-type.service';
import { CompanyTypeEditComponent } from './company-type-edit/company-type-edit.component';

@Component({
  selector: 'app-company-company-type-list',
  templateUrl: './company-type-list.component.html',
})
export class CompanyTypeListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  constructor(private message: NzMessageService,
              private route:ActivatedRoute,
              private companyTypeService:CompanyTypeService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
      this.initPage();
  }

  initPage() {
    this.isLoading = true;
    this.companyTypeService.pagination(this.page, this.keyword).then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(CompanyTypeEditComponent, { entity: bak}, 600, {
      nzTitle:'活动类型信息',
    }).subscribe(result => {
      if(result){
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };


  delete = (data) => {
    // this.bannerService.deleteObj(data.id).then((data) => {
    //   this.search();
    //   this.message.success('删除成功');
    // }, error => console.log('删除出错，后续处理'));
  };


}
